package org.dftproject.gedcom.core;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class GedcomManager {

	private static GedcomManager singleton = new GedcomManager();

	private final Map<String, Gedcom> gedcoms = new HashMap<String, Gedcom>();

	private GedcomManager() {
	}

	public static GedcomManager getDefault() {
		return singleton;
	}

	public Gedcom getGedcom(String path) {
		// Get the unique key for this GEDCOM

		try {
			path = new File(path).getCanonicalPath();
		} catch (IOException e) {
			return null;
		}

		// Get the GEDCOM, loading it if neccessary

		Gedcom gedcom = gedcoms.get(path);
		if (gedcom == null) {
			synchronized (gedcoms) {
				gedcom = gedcoms.get(path);
				if (gedcom == null) {
					gedcom = new Gedcom();
					gedcom.load(path);
					gedcoms.put(path, gedcom);
				}
			}
		}

		return gedcom;
	}

}
